﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace CAPTS
{
    internal class DataLine
    {

        // data used for linear fit, already delete prefix and fixed the length.
        public string time_string;// time in string, if for uV, NA. 
        public int seconds; // time in unit of second, if for uV, NA. 
        public double co2; // ppm, or uV
        public double temperature;     // oC, or uV
        public double air_pressure;    // kPa, or uV
        public double ppfd;            // micro mol.m-2.s-1, or uV
        public double relative_humidity;// % (0-100), or uV
        public double water_vapor;     // ppt, or uV

        public void copyData(string time_str, int sec, double c, double t, double p, double ppf, double rh, double h2o)
        {
            time_string = time_str;
            seconds = sec;
            co2 = c;
            temperature = t;
            air_pressure = p;
            ppfd = ppf;
            relative_humidity = rh;
            water_vapor = h2o;
        }
    }
    public class Logger
    {
        private static readonly string logFilePath = Path.Combine(@"D:\Error", "error.log");

        public static void Log(string message)
        {
            try
            {
                string logDirectory = Path.GetDirectoryName(logFilePath);
                if (!Directory.Exists(logDirectory))
                {
                    Directory.CreateDirectory(logDirectory);
                }
                using (StreamWriter writer = new StreamWriter(logFilePath, true))
                {
                    writer.WriteLine($"{DateTime.Now:yyyy-MM-dd HH:mm:ss} - {message}");
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"日志写入失败: {ex.Message}");
            }
        }
    }


    static class Program
    {
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main()
        {
            try
            {
                //设置应用程序处理异常方式：ThreadException处理
                Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
                //处理UI线程异常
                Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);
                //处理非UI线程异常
                AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);

                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Application.Run(new Form1());
            }
            catch (Exception ex)
            {
            }
        }
        static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
        {
            // 捕获 UI 线程中的未处理异常
            Logger.Log($"UI线程异常: {e.Exception.Message}\n堆栈信息: {e.Exception.StackTrace}");
        }

        static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            // 捕获非 UI 线程中的未处理异常
            var exception = e.ExceptionObject as Exception;
            Logger.Log($"非UI线程异常: {exception?.Message}\n堆栈信息: {exception?.StackTrace}");
            Application.Exit();
        }
    }
}
